﻿<%@ Application Language="C#" %>
<%@ Import Namespace="Shopping.Common" %>
<%@ Import Namespace="Shopping.BLL" %>
<%@ Import Namespace = "System.Collections.Generic" %>
<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        //在应用程序启动时运行的代码

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //在应用程序关闭时运行的代码

    }

    string GetCookies(string key)
    {
        if (Request.Cookies["SensisWebSystem"] == null || Request.Cookies["SensisWebSystem"].Values[key] == null)
            return "";
        return HttpUtility.UrlDecode(Request.Cookies["SensisWebSystem"].Values[key] + "", System.Text.Encoding.Unicode);
    }
    private static EmployeesManage bllEmployee = new EmployeesManage();
    private static UsersManage bllUser = new UsersManage();
    void Application_Error(object sender, EventArgs e)
    {
      
        Exception ex = Server.GetLastError().GetBaseException();

        System.Text.StringBuilder sb = new StringBuilder();

        sb.AppendLine("发生时间：" + DateTime.Now.ToString() + "");
        sb.AppendLine("发生异常页：" + Request.Url.ToString() + "");
        sb.AppendLine("异常信息：" + ex.Message + "");
        sb.AppendLine("错误源：" + ex.Source + "");
        sb.AppendLine("IP地址：" + Utility.GetClientIPAddress());
        sb.AppendLine("会员身份：" + Utility.GetCookies(CConst.Cookie_User_Name));
        sb.AppendLine("管理员身份：" + Utility.GetCookies(CConst.Cookie_Employee_Name));
        
        List<string> listOfPost = new List<string>();
        for (int i = 0; i < Request.Form.Count; ++i)
        {
            listOfPost.Add(Request.Form.Keys[i].ToString() + "=" + Request.Form[i].ToString());
        }
        sb.AppendLine("Post信息：" + string.Join("&", listOfPost.ToArray()));
        sb.AppendLine("堆栈信息：" + ex.StackTrace + "");
        sb.AppendLine("--------------------------------------------------------------------------------------------------------------");

        Server.ClearError();

        //写入错误日志 
        string path = Server.MapPath("~/ErrorLog/" + DateTime.Now.ToString("yyyyMM"));
        string filename =  "/errlog_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
        Utility.WriteToFile(path, filename, sb.ToString());
      
    }

    void Session_Start(object sender, EventArgs e) 
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e) 
    {
        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时，才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer，则不会引发该事件。

    }
    
    
</script>
